<?php


namespace App\Commons\Domain\Module\Exchange\Entity;

use App\Commons\Domain\Core\DomainEntity;
use App\Commons\Domain\Module\Exchange\Model\Advertising;
use Arr;

class AdvertisingEntity extends DomainEntity
{
    public const ENTITY = Advertising::class;

    /**
     * 创建广告
     * @param string $uid
     * @param int $wallet_id
     * @param float $price 单枚币的价格
     * @param float $quantity 数量
     * @param int $type 广告类型
     * @param array $data 其他数据:['min_limit'=>100, 'max_limit'=>100, 'pay_time_limit'=>5, 'remark','pay_type'];
     * @return mixed
     */
    public function createAd(string $uid, int $wallet_id, float $price, float $quantity, int $type, array $data)
    {
        return db_trans(function () use ($uid, $wallet_id, $price, $quantity, $type, $data) {
            $ad_data = Arr::only($data, [
                'min_limit',
                'max_limit',
                'pay_time_limit',
                'remark',
                'country_id',
                'pay_type'
            ]);
            throw_on(bcmul($price, $quantity, 6) < $ad_data['max_limit'], 0xf00312);
            return $this->create(compact('uid', 'wallet_id', 'price', 'quantity', 'type') + $ad_data + ['status' => 1]);
        });
    }

    /**
     * 更新广告
     * @param int $ad_id
     * @param array $data
     * @return mixed
     */
    public function updateAd(int $ad_id, array $data)
    {
        $ad = $this->whereId($ad_id)->lockForUpdate()->firstOrFail();
        $ad->orders->each(function ($item, $key) {
            throw_on($item->status === 1, 0xf00271);
        });

        $ad_data = Arr::only($data, [
            'country_id',
            'price',
            'quantity',
            'pay_time_limit',
            'min_limit',
            'max_limit',
            'remark',
        ]);

        return $ad->update($ad_data);
    }

    /**
     * 上架下架广告
     * @param int $ad_id
     * @param int $status
     * @return mixed
     */
    public function upDown(int $ad_id, int $status)
    {
        $ad = $this->whereId($ad_id)->lockForUpdate()->firstOrFail();
        $ad->orders->each(function ($item, $key) {
            throw_on($item->status === 1, 0xf00271);
        });

        return $ad->update(['status' => $status]);
    }

    /**
     * 减少广告数量
     * @param int $ad_id
     * @param float $amount
     * @return mixed
     */
    public function cutAd(int $ad_id, float $amount)
    {
        $ad = $this->whereId($ad_id)->lockForUpdate()->firstOrFail();
        throw_on($ad->quantity < $amount, 0xf00292);
        $ad->quantity = $ad->quantity - $amount;

        // 减少后的数量是否涉及广告上限调整
        if (bcmul($ad->quantity, $ad->price, 6) < $ad->max_limit) {
            $ad->max_limit = bcmul($ad->quantity, $ad->price, 6);
        }
        return $ad->save();
    }

    /**
     * 增加广告数量
     * @param int $ad_id
     * @param float $amount
     * @return mixed
     */
    public function addAd(int $ad_id, float $amount)
    {
        $ad = $this->whereId($ad_id)->lockForUpdate()->firstOrFail();
        $ad->quantity = $ad->quantity + $amount;
        return $ad->save();
    }

}
